#include "ImaRequestProcessor.h"
#include <stdio.h>
#include <string.h>
#include "ImaGlobals.h"
#include "ImaConstants.h"
#include "ImaRequestValidator.h"
#include "ImaUtils.h"

ImaRequestProcessor::ImaRequestProcessor() {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::ImaRequestProcessor() called");
}

ImaRequestProcessor::ImaRequestProcessor(ImaRequestProcessor& imaReqProcObj) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::ImaRequestProcessor copy constructor called");
}

int ImaRequestProcessor::processFeedbackRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processFeedbackRequest called");	

	int retVal = 0;
	const char *pSenderName, *pSenderEmailId, *pMessageSub, *pMessageBody;
	char log_msg[512];

	json_t* pSenderNameObject = json_object_get(pRoot, IMA_JSON_KEY_NAME);
	if(pSenderNameObject != NULL) {
		if(json_is_string(pSenderNameObject) == true) {
			pSenderName = json_string_value(pSenderNameObject);
		} else {
			sprintf(log_msg, "Not a valid sender name for the feedback request.");
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "The sender's name doesn't exist");
		messagesVector.push_back(new std::string("The sender's name doesn't exist"));
		retVal = -1;
	}

	json_t* pSenderEmailObject = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pSenderEmailObject != NULL) {
		if(json_is_string(pSenderEmailObject) == true) {
			pSenderEmailId = json_string_value(pSenderEmailObject);
		} else {
			sprintf(log_msg, "Not a valid sender email for the feedback request.");
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "The sender's email-id doesn't exist");
		messagesVector.push_back(new std::string("The sender's email-id doesn't exist"));
		retVal = -1;
	}

	json_t* pMsgSubObject = json_object_get(pRoot, IMA_JSON_KEY_SUBJECT);
	if(pMsgSubObject != NULL) {
		if(json_is_string(pMsgSubObject) == true) {
			pMessageSub = json_string_value(pMsgSubObject);
		} else {
			sprintf(log_msg, "Not a valid message subject for the feedback request.");
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "The feddback message subject doesn't exist");
		messagesVector.push_back(new std::string("The feddback message subject doesn't exist"));
		retVal = -1;
	}
	

	json_t* pMsgBodyObject = json_object_get(pRoot, IMA_JSON_KEY_BODY);
	if(pMsgBodyObject != NULL) {
		if(json_is_string(pMsgBodyObject) == true) {
			pMessageBody = json_string_value(pMsgBodyObject);
		} else {
			sprintf(log_msg, "Not a valid message body for the feedback request.");
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "The feedback message body doesn't exist");
		messagesVector.push_back(new std::string("The feedback message body doesn't exist."));
		retVal = -1;
	}

	if(retVal != 0) {
		LOG(debug, __FILE__, __LINE__, "All the required parameters don't exist. Returning from here.");
		return retVal;
	}

	if(gpMqInterface->enqueueUserFeedback(pSenderName, pSenderEmailId, pMessageSub, pMessageBody)) {
		sprintf(log_msg, "Your feedback has been received");
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		sprintf(log_msg, "Failed to receive user feedback.");
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}
	
	return retVal;
}

int ImaRequestProcessor::processRegistrationRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, string& userId) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processRegistrationRequest called");

	int retVal = 0;

   json_t* pRegnDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pRegnDetailsObject == NULL) {
      LOG(error, __FILE__, __LINE__, "Registration details object not present in JSON string");
		messagesVector.push_back(new std::string("Registration details object not present in JSON string"));
      return -1;
   } else {
      if(json_is_object(pRegnDetailsObject) == false) {
         LOG(error, __FILE__, __LINE__, "Registration details info not in correct format");
			messagesVector.push_back(new std::string("Registration details info not in correct format"));
         return -1;
      }
   }

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	/* Extract and validate the password */
	const char* pPassword = NULL;
	json_t* pPasswordObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_PASSWORD);
	if(pPasswordObj == NULL) {
		messagesVector.push_back(new std::string("Password field doesn't exist"));
		retVal = -1;
	} else {
		if(json_is_string(pPasswordObj)) {
			pPassword = json_string_value(pPasswordObj);
			if(strlen(pPassword) < 4) {
				messagesVector.push_back(new std::string("Password length should be minimum 4 charatcters"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Invalid Password"));
			retVal = -1;
		}
	}

	/* Extract and validate the Name of the user - Not a mandatory field */
	const char* pName = "";
	json_t* pNameObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_NAME);
	if(pNameObj != NULL) {
		if(json_is_string(pNameObj)) {
			pName = json_string_value(pNameObj);
			if(strlen(pName) < 4) {
				messagesVector.push_back(new std::string("Name should be of minimum 4 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid Name"));
			retVal = -1;
		} 
	}

	/* Extract and validate the gender - Not a mandatory field*/
	const char* pGender = NULL;
	char gender = 'U';
	json_t* pGenderObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_GENDER);
	if(pGenderObj != NULL) {
		if(json_is_string(pGenderObj)) {
			pGender = json_string_value(pGenderObj);
			if((pGender[0] != 'm') && (pGender[0] != 'M')
					&& (pGender[0] != 'f') && (pGender[0] != 'F')
					&& (pGender[0] != 'u') && (pGender[0] != 'U')) {
				messagesVector.push_back(new std::string("Not a valid gender."));
				retVal = -1;
			} else {
				gender = pGender[0];
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid gender."));
			retVal = -1;
		}
	}

	/* Extract and validate the address field */
	const char* pAddr = "";
	json_t* pAddrObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_ADDRESS);
	if(pAddrObj != NULL) {
		if(json_is_string(pAddrObj)) {
			pAddr = json_string_value(pAddrObj);
			if(strlen(pAddr) < 4) {
				messagesVector.push_back(new std::string("Address should be of minimum 4 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid Address"));
			retVal = -1;
		}
	}

	/*Extract and validate the city field */
	const char* pCity = "";
	json_t* pCityObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_CITY);
	if(pCityObj != NULL) {
		if(json_is_string(pCityObj)) {
			pCity = json_string_value(pCityObj);
			if(strlen(pCity) < 2) {
				messagesVector.push_back(new std::string("City should be of minimum 2 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid city"));
			retVal = -1;
		}
	}
	
	/*Extract and validate the state field */
	const char* pState = "";
	json_t* pStateObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_STATE);
	if(pStateObj != NULL) {
		if(json_is_string(pStateObj)) {
			pState = json_string_value(pStateObj);
			if(strlen(pState) < 2) {
				messagesVector.push_back(new std::string("State should be of minimum 2 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid state"));
			retVal = -1;
		}
	}
 
	/* Extract and validate the country field */
	const char* pCountry = "";
	json_t* pCountryObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_COUNTRY);
	if(pCountryObj != NULL) {
		if(json_is_string(pCountryObj)) {
			pCountry = json_string_value(pCountryObj);
			if(strlen(pCountry) < 2) {
				messagesVector.push_back(new std::string("COuntry should be of minimum 2 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid country"));
			retVal = -1;
		}
	}

	/* Extract and validate the zip field */
	const char* pZip = "";
	json_t* pZipObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_ZIP);
	if(pZipObj != NULL) {
		if(json_is_string(pZipObj)) {
			pZip = json_string_value(pZipObj);
			if(strlen(pZip) < 2) {
				messagesVector.push_back(new std::string("Zip should be of minimum 2 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid zip"));
			retVal = -1;
		}
	} 

	/* Extract and validate the phone num field */
	const char* pPhone = "";
	json_t* pPhoneObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_PHONE);
	if(pPhoneObj != NULL) {
		if(json_is_string(pPhoneObj)) {
			pPhone = json_string_value(pPhoneObj);
			if(strlen(pPhone) < 2) {
				messagesVector.push_back(new std::string("Phone number should be of minimum 2 characters."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid Phone number"));
			retVal = -1;
		}
	}
 
	/* Extract and validate the DOB field */
	const char* pDob = ""; //DOB format should be yyyy-mm-dd
	json_t* pDobObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_DOB);
	if(pDobObj != NULL) {
		if(json_is_string(pDobObj)) {
			pDob = json_string_value(pDobObj);
			if(strlen(pDob) < 6) {	//TODO: validate date field in a better way
				messagesVector.push_back(new std::string("Not a valid date of birth"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid date of birth"));
			retVal = -1;
		}
	} 

	/* Extract and validate the IM-ID and IM-Service field */
	const char* pImId = ""; const char* pImService = "";
	
	json_t* pImIdObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_IM_ID);
	if(pImIdObj != NULL) {
		if(json_is_string(pImIdObj)) {
			pImId = json_string_value(pImIdObj);
			if(strlen(pImId) < 1) {
				messagesVector.push_back(new std::string("Not a valid IM ID"));
				retVal = -1;
			} /* else {	//Also fetch the IM service name
				json_t* pImServiceObj = json_object_get(pRegnDetailsObject, IMA_JSON_KEY_IM_SERVICE);
				if(pImServiceObj != NULL) {
					if(json_is_string(pImServiceObj)) {
						pImService = json_string_value(pImServiceObj);
						if(strlen(pImService) < 1) {
					 		messagesVector.push_back(new std::string("If IM ID is there, IM service name can't be empty"));
							retVal = -1;
						}
					}
				} else {
					 messagesVector.push_back(new std::string("If IM ID is there, IM service name can't be empty"));
					retVal = -1;
				}	
			} */
		} else {
			messagesVector.push_back(new std::string("Not a valid IM Id"));
			retVal = -1;
		}
	} 

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->isRegisteredUser(pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The user id %s is already registered", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	} else {
		char confCode[128];
		ImaUtils::genRandomKey(confCode, 6);
		if(0 >  gpDbInterface->registerNewUser(pEmailId, pPassword, 
						pName, gender, pAddr, pCity, pState, pCountry, 
						pZip, pPhone, pDob, pImId, pImService, confCode)) {
			char log_msg[1024];
			sprintf(log_msg, "Failed to register new user %s because of DB connection failure", pEmailId);
			LOG(error, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "Registration of email id %s success", pEmailId);
			LOG(info, __FILE__, __LINE__, log_msg);
			//Enqueue the conf code so that the user may confirm registration	
			bool bEMailAndImSame = true;
			if(strcmp(pEmailId, pImId) == 0) {
				bEMailAndImSame = true;
			} else {
				bEMailAndImSame = false;
			}
			if(false == gpMqInterface->enqueueConfCode(pEmailId, confCode, bEMailAndImSame)) {
				sprintf(log_msg, "Failed to enqueue registration confirmation code : %s into the message queue", confCode);
				LOG(error, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = -1;
			}

			retVal = 0;
		}
	}
 
	return retVal;
}

int ImaRequestProcessor::processConfirmRegistrationRequest(json_t* pRoot, std::vector<std::string*>& messagesVector){
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processConfirmRegistrationRequest called");	
	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	//TODO: check if the user is already registered or not.

	if(gpDbInterface->isRegisteredUser(pEmailId) != 1) {
		messagesVector.push_back(new std::string("Not a registered user"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}
	
	if(1 == gpDbInterface->confirmUserRegistration(pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The registration of user id %s confirmed", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = 0;
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to confirm registration of new user %s because of DB error", 
			pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processUpdateUserDetailsRequest(json_t* pRoot, std::vector<std::string*>& messagesVector){
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processUpdateUserDetailsRequest called");

	int retVal = 0;
	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	/* Now check if the user Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredUser(pEmailId)) {
		sprintf(log_msg, "User ID %s is registered", pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "User id %s is not registered.", pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(debug, __FILE__, __LINE__, "User details object not present in JSON string");
		messagesVector.push_back(new std::string("User Details object not present in JSON string"));
      retVal = -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(debug, __FILE__, __LINE__, "User details not in correct format");
			messagesVector.push_back(new std::string("User Details not in correct format"));
         retVal = -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	const char* pImId = NULL;
	const char* pName = NULL;
	const char* pAddress = NULL;
	const char* pCity = NULL;
	const char* pState = NULL;
	const char* pCountry = NULL;
	const char* pZip= NULL;
	const char* pPhone = NULL;
	const char* pGender = NULL;
	const char* pDob = NULL;

	if(0 != extractUserDetails(pDetailsObject, pImId, pName, pAddress, pCity, pState, pCountry, pZip, pPhone, pGender, pDob, messagesVector)) {
		retVal = -1;
		LOG(debug, __FILE__, __LINE__, "Required user details not present");
		messagesVector.push_back(new std::string("Required user details not present"));
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->updateUserDetails(pEmailId, pImId, pName, pAddress, pCity, pState, pCountry, pZip, pPhone, pGender, pDob)) {
		char log_msg[512];
		sprintf(log_msg, "The details of of user %s updated successfully", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);

		//Also update the last access time of the user
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Failed to update last access time of the user");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The details of user %s failed to be updated", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::extractUserDetails(json_t* pDetailsObject, const char*& pImId, const char*& pName, const char*& pAddress, const char*& pCity, const char*& pState, const char*& pCountry, const char*& pZip, const char*& pPhone, const char*& pGender, const char*& pDob, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::extractUserDetails called");	

	int retVal = 0;

	/* Extract the IM ID*/
	json_t* pImIdObj = json_object_get(pDetailsObject, IMA_JSON_KEY_IM_ID);
	if(pImIdObj == NULL) {
		messagesVector.push_back(new std::string("User's IMID field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pImIdObj)) {
			pImId = json_string_value(pImIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid IM ID"));
			retVal = -1;
		}
	}
	
	/* Extract the name*/
	json_t* pNameObj = json_object_get(pDetailsObject, IMA_JSON_KEY_NAME);
	if(pNameObj == NULL) {
		messagesVector.push_back(new std::string("User's Name field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pNameObj)) {
			pName = json_string_value(pNameObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid name"));
			retVal = -1;
		}
	}

	/* Extract the Address*/
	json_t* pAddressObj = json_object_get(pDetailsObject, IMA_JSON_KEY_ADDRESS);
	if(pAddressObj == NULL) {
		messagesVector.push_back(new std::string("User's Address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pAddressObj)) {
			pAddress = json_string_value(pAddressObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid user Address"));
			retVal = -1;
		}
	}
	
	/* Extract the City*/
	json_t* pCityObj = json_object_get(pDetailsObject, IMA_JSON_KEY_CITY);
	if(pCityObj == NULL) {
		messagesVector.push_back(new std::string("User's City field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pCityObj)) {
			pCity = json_string_value(pCityObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid user city"));
			retVal = -1;
		}
	}

	/* Extract the State*/
	json_t* pStateObj = json_object_get(pDetailsObject, IMA_JSON_KEY_STATE);
	if(pStateObj == NULL) {
		messagesVector.push_back(new std::string("User's state field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pStateObj)) {
			pState = json_string_value(pStateObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid user state"));
			retVal = -1;
		}
	}

	/* Extract the Country*/
	json_t* pCountryObj = json_object_get(pDetailsObject, IMA_JSON_KEY_COUNTRY);
	if(pCountryObj == NULL) {
		messagesVector.push_back(new std::string("User's country field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pCountryObj)) {
			pCountry = json_string_value(pCountryObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid user country"));
			retVal = -1;
		}
	}

	/* Extract the Zip*/
	json_t* pZipObj = json_object_get(pDetailsObject, IMA_JSON_KEY_ZIP);
	if(pZipObj == NULL) {
		messagesVector.push_back(new std::string("User's zip field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pZipObj)) {
			pZip = json_string_value(pZipObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid zip"));
			retVal = -1;
		}
	}

	/* Extract the Phone number*/
	json_t* pPhoneObj = json_object_get(pDetailsObject, IMA_JSON_KEY_PHONE);
	if(pPhoneObj == NULL) {
		messagesVector.push_back(new std::string("User's phone field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pPhoneObj)) {
			pPhone = json_string_value(pPhoneObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid phone no."));
			retVal = -1;
		}
	}

	/* Extract the gender*/
	json_t* pGenderObj = json_object_get(pDetailsObject, IMA_JSON_KEY_GENDER);
	if(pGenderObj == NULL) {
		messagesVector.push_back(new std::string("User's gender field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pGenderObj)) {
			pGender = json_string_value(pGenderObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid gender"));
			retVal = -1;
		}
	}
	
	/* Extract the DOB*/
	json_t* pDobObj = json_object_get(pDetailsObject, IMA_JSON_KEY_DOB);
	if(pDobObj == NULL) {
		messagesVector.push_back(new std::string("User's DOB field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pDobObj)) {
			pDob = json_string_value(pDobObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid DOB"));
			retVal = -1;
		}
	}

	return retVal;
}

int ImaRequestProcessor::processLoginRequest(json_t* pRoot, std::vector<std::string*>& messagesVector){
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processLoginRequest called");	

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	/* Extract and validate the user password */
	const char* pPassword= NULL;
	json_t* pPasswordObj = json_object_get(pRoot, IMA_JSON_KEY_PASSWORD);
	if(pPasswordObj == NULL) {
		messagesVector.push_back(new std::string("Password field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pPasswordObj)) {
			pPassword = json_string_value(pPasswordObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid password"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	if(1 == gpDbInterface->areUserCredentialsCorrect(pEmailId, pPassword)) {
		char log_msg[512];
		sprintf(log_msg, "The credentials of user id %s are correct", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = 0;
		
		if(gpDbInterface->getUserRegistrationStatus(pEmailId) != IMA_REGN_CONFIRMED) {
			if(confirmRegistration(pRoot, pEmailId) != 0) {
				char log_msg[512];
				sprintf(log_msg, "The conf code of user id %s is incorrect", pEmailId);
				LOG(info, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = IMA_RESPONSE_REGISTRATION_CONFIRMATION_PENDING;
			} else {
				sprintf(log_msg, "The conf code of user id %s is correct", pEmailId);
			}
		} else {
			//Also update the last access time
			if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
				LOG(error, __FILE__, __LINE__, "Failed to update last access time");
			}
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The credentials of user %s are incorrect", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::confirmRegistration(json_t* pRoot, const char* pEmailId) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::confirmRegistration called");
	int retVal = 0;
	const char* pConfCode = NULL;
	json_t* pConfCodeObj = json_object_get(pRoot, IMA_JSON_KEY_CONFCODE);
	if(pConfCodeObj == NULL) {
		LOG(debug, __FILE__, __LINE__, "Conf code not available");
		retVal = -1;
	} else {
		if(json_is_string(pConfCodeObj)) {
			pConfCode = json_string_value(pConfCodeObj);
		} else {
			LOG(debug, __FILE__, __LINE__, "Not a valid conf code");
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	if(gpDbInterface->isUserRegnConfCodeCorrect(pEmailId, pConfCode) == 1) {
		LOG(debug, __FILE__, __LINE__, "Conf code of the user is correct");
		if(gpDbInterface->confirmUserRegistration(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Updated the registration status of the user to confirmed");
			if(enqueueImIdForFriendRequest(pEmailId) == 0) {
				char log_msg[1024];
				sprintf(log_msg, "IM ID for %s enqueued into the message queue", pEmailId);
				LOG(debug, __FILE__, __LINE__, log_msg);
			} else {
				char log_msg[1024];
				sprintf(log_msg, "Failed to enqueue IM ID for %s into the message queue", pEmailId);
				LOG(debug, __FILE__, __LINE__, log_msg);
			}
		} else {
			LOG(error, __FILE__, __LINE__, "Failed to update the registration status of the user to confirmed");
			retVal = -1;
		}
	} else {
		LOG(debug, __FILE__, __LINE__, "Conf code of the user is incorrect");
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::enqueueImIdForFriendRequest(const char* pEmailId) {
	LOG(debug, __FILE__, __LINE__, "enqueueImIdForFriendRequest called");
	int retVal = 0;
	std::string strImId;
	if(gpDbInterface->fetchImId(pEmailId, strImId)) {
		char log_msg[1024];
		sprintf(log_msg, "IM ID fetched for user %s from DB is %s", pEmailId, strImId.c_str());
		LOG(debug, __FILE__, __LINE__, log_msg);
		if(strcmp(pEmailId, strImId.c_str()) == 0) {
			if(ImaUtils::isValidImId(strImId.c_str())) {
				char log_msg[1024];
				sprintf(log_msg, "IM ID %s is valid", strImId.c_str());
				LOG(debug, __FILE__, __LINE__, log_msg);
				//enqueue the user's imid into the message queue so that the same can be sent a friend request
				if(false == gpMqInterface->enqueueImId(strImId.c_str())) {
					sprintf(log_msg, "Failed to enqueue IM ID : %s into the message queue", strImId.c_str());
					LOG(error, __FILE__, __LINE__, log_msg);
					retVal = -1;
				} else {
					sprintf(log_msg, "IM ID %s enqueued into the message queue", strImId.c_str());
					LOG(debug, __FILE__, __LINE__, log_msg);
					if(!gpDbInterface->setBuddyAddedStatus(pEmailId, "S")) {
						LOG(error, __FILE__, __LINE__, "Failed to set buddy_added_status into the DB");
					}
				}
			} else {
				sprintf(log_msg, "IM ID %s is not valid", strImId.c_str());
				LOG(debug, __FILE__, __LINE__, log_msg);
				retVal = -1;
			}
		} else {
			LOG(debug, __FILE__, __LINE__, "email id and im id are not same, so we wil not send friend request");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch IM ID for user %s", pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = -1;
	}

	return retVal;
}


int ImaRequestProcessor::processAddInfoRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, std::string& strInfoId) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processAddInfoRequest called");

	int retVal = 0;
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}
	
//	/* Extract and validate the user id*/
//	unsigned long userId;
//	json_t* pUserIdObj = json_object_get(pRoot, IMA_JSON_KEY_USERID);
//	if(pUserIdObj == NULL) {
//		messagesVector.push_back(new std::string("User ID field not existing."));
//		retVal = -1;
//	} else {
//		if(json_is_integer(pUserIdObj)) {
//			userId = json_integer_value(pUserIdObj);
//			if(!gpDbInterface->isRegisteredUser(userId)) {
//				messagesVector.push_back(new std::string("Not a registered user"));
//				retVal = -1;
//			}
//		} else {
//			messagesVector.push_back(new std::string("Not a valid User id"));
//			retVal = -1;
//		}
//	}

   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(error, __FILE__, __LINE__, "Info details object not present in JSON string");
		messagesVector.push_back(new std::string("Info details object not present in JSON string"));
      return -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(error, __FILE__, __LINE__, "Info details info not in correct format");
			messagesVector.push_back(new std::string("Info details not in correct format"));
         return -1;
      }
   }

	/* Extract the Information*/
	const char* pInfo = NULL;
	json_t* pInfoObj = json_object_get(pDetailsObject, IMA_JSON_KEY_INFO);
	if(pInfoObj == NULL) {
		messagesVector.push_back(new std::string("info field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pInfoObj)) {
			pInfo = json_string_value(pInfoObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid info"));
			retVal = -1;
		}
	}

	/* Extract the media type i.e. plain-text/text-file/picture-file/audio-file/video-file etc. */
	int media = -1;
	json_t* pMediaObj = json_object_get(pDetailsObject, IMA_JSON_KEY_MEDIA);
	if(pMediaObj == NULL) {
		messagesVector.push_back(new std::string("The media of information not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pMediaObj)) {
			media = json_integer_value(pMediaObj);
			if(media < IMA_INFO_MEDIA_TEXT || media > IMA_INFO_MEDIA_VIDEO_FILE) {
				messagesVector.push_back(new std::string("Not a valid info media type."));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid info media type"));
			retVal = -1;
		}
	}

	/* Extract the expiry date of the information */
	const char* pExpiryDate = NULL;
	json_t* pExpiryDateObj = json_object_get(pDetailsObject, IMA_JSON_KEY_EXPIRY_DATE);
	if(pExpiryDateObj == NULL) {
		messagesVector.push_back(new std::string("The expiry date of information not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pExpiryDateObj)) {
			pExpiryDate = json_string_value(pExpiryDateObj);
			//TODO : validate date
		} else {
			messagesVector.push_back(new std::string("Not a valid date"));
			retVal = -1;
		}
	}

	/* Extract the information TTL */	
	unsigned long ttl = -1;
	json_t* pTtlObj = json_object_get(pDetailsObject, IMA_JSON_KEY_TTL);
	if(pTtlObj == NULL) {
		messagesVector.push_back(new std::string("The TTL of info not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pTtlObj)) {
			ttl = json_integer_value(pTtlObj);
			if(ttl < 0) {
				messagesVector.push_back(new std::string("TTL cannot be -ve"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid TTL"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->addInfo(pEmailId, pInfo, media, pExpiryDate, ttl)) {
		char log_msg[512];
		sprintf(log_msg, "The information %s successfully added for %s", pInfo, pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
//		messagesVector.push_back(new std::string(log_msg));

		//Fetch the info id
		if(gpDbInterface->fetchInfoId(pEmailId, pInfo, media, pExpiryDate, ttl, strInfoId) == 0) {
			char log_msg[1024];
			sprintf(log_msg, "Failed to fetch info id for info %s added by user %s", pInfo, pEmailId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			retVal = -1;
		} else {
			char log_msg[1024];
			sprintf(log_msg, "info id for info %s added by user %s is %s", pInfo, pEmailId, strInfoId.c_str());
			LOG(debug, __FILE__, __LINE__, log_msg);
		}

		//Also update the last access time of the user
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Failed to update last access time of the user");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The information %s failed to be added for %s", pInfo, pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processUpdateInfoRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processUpdateInfoRequest called");

	int retVal = 0;
	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Extract the Information Id*/
	unsigned long infoId = 0;
	json_t* pInfoIdObj = json_object_get(pRoot, IMA_JSON_KEY_INFO_ID);
	if(pInfoIdObj == NULL) {
		messagesVector.push_back(new std::string("info id field not existing."));
		LOG(debug, __FILE__, __LINE__, "info id field not existing.");
		retVal = -1;
	} else {
		if(json_is_integer(pInfoIdObj)) {
			infoId = json_integer_value(pInfoIdObj);
			sprintf(log_msg, "Info id is %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
		} else {
			messagesVector.push_back(new std::string("Not a valid info Id"));
			LOG(debug, __FILE__, __LINE__, "Not a valid info id");
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the info Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredInfo(infoId, pEmailId)) {
		sprintf(log_msg, "Info id %ld belonging to owner %s is registered", infoId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
	} else {
		sprintf(log_msg, "Info id %ld is not valid.", infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid info-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(error, __FILE__, __LINE__, "Info details object not present in JSON string");
		messagesVector.push_back(new std::string("Info details object not present in JSON string"));
      return -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(error, __FILE__, __LINE__, "Info details info not in correct format");
			messagesVector.push_back(new std::string("Info details not in correct format"));
         return -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	/* Extract the Information*/
	const char* pInfo = NULL;
	json_t* pInfoObj = json_object_get(pDetailsObject, IMA_JSON_KEY_INFO);
	if(pInfoObj == NULL) {
		messagesVector.push_back(new std::string("info field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pInfoObj)) {
			pInfo = json_string_value(pInfoObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid info"));
			retVal = -1;
		}
	}

	/* Extract the expiry date of the information */
	const char* pExpiryDate = NULL;
	json_t* pExpiryDateObj = json_object_get(pDetailsObject, IMA_JSON_KEY_EXPIRY_DATE);
	if(pExpiryDateObj == NULL) {
		messagesVector.push_back(new std::string("The expiry date of information not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pExpiryDateObj)) {
			pExpiryDate = json_string_value(pExpiryDateObj);
			//TODO : validate date
		} else {
			messagesVector.push_back(new std::string("Not a valid date"));
			retVal = -1;
		}
	}

	/* Extract the information TTL */	
	unsigned long ttl = -1;
	json_t* pTtlObj = json_object_get(pDetailsObject, IMA_JSON_KEY_TTL);
	if(pTtlObj == NULL) {
		messagesVector.push_back(new std::string("The TTL of info not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pTtlObj)) {
			ttl = json_integer_value(pTtlObj);
			if(ttl < 0) {
				messagesVector.push_back(new std::string("TTL cannot be -ve"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid TTL"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->updateInfo(infoId, pInfo, pExpiryDate, ttl)) {
		char log_msg[512];
		sprintf(log_msg, "The information %s successfully updated for %ld", pInfo, infoId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = 0;

		//Also update the last access time
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(error, __FILE__, __LINE__, "Failed to update last access time");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The information %s failed to be updated for %ld", pInfo, infoId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

	//disassociate the recipients: begin
	json_t* pRecipientsToDisassociateObj = json_object_get(pDetailsObject, IMA_JSON_KEY_RECIPIENTSTODISASSOCIATE);
	if(json_is_array(pRecipientsToDisassociateObj) == true) {
		size_t noOfElements = json_array_size(pRecipientsToDisassociateObj);
		if(noOfElements >= 1) {
			for(unsigned int i = 0; i < noOfElements; i++) {
				json_t* pRecipientIdObj = json_array_get(pRecipientsToDisassociateObj, i);
				if((pRecipientIdObj != NULL) && (json_is_integer(pRecipientIdObj) == true)) {
					unsigned long recipientId = json_integer_value(pRecipientIdObj);
					if(1 == gpDbInterface->isRegisteredRecipient(recipientId, pEmailId)) {
						if(1 != gpDbInterface->deleteInfoRecipient(recipientId, infoId)) {
							char log_msg[1024];
							sprintf(log_msg, "Failed to disaasociate recipient %lu from %lu", recipientId, infoId);
							LOG(error, __FILE__, __LINE__, log_msg);
							messagesVector.push_back(new std::string(log_msg));
							retVal = -1;
							break;
						}
					}
				}
			}
		}
	}
	//end
	
	return retVal;
}

int ImaRequestProcessor::processDeleteInfoRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processDeleteInfoRequest called");

	char log_msg[1024];
	int retVal = 0;
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	/* Extract the Information Id*/
	long infoId = 0;
	json_t* pInfoIdObj = json_object_get(pRoot, IMA_JSON_KEY_INFO_ID);
	if(pInfoIdObj == NULL) {
		messagesVector.push_back(new std::string("info id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pInfoIdObj)) {
			infoId = json_integer_value(pInfoIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid info Id"));
			retVal = -1;
		}
	}
	
	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the info Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredInfo(infoId, pEmailId)) {
		sprintf(log_msg, "Info id %ld belonging to owner %s is registered", infoId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Info id %ld is not valid.", infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid info-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->deleteInfo(infoId, pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The information %ld successfully deleted from the DB", infoId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = 0;
		//Also update the last access time
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(error, __FILE__, __LINE__, "Failed to update last access time");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The information %ld failed to be deleted because of some DB error", infoId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::extractRecipientInfo(const unsigned long& infoId, const json_t* pRecipientObject, unsigned long& recipientId, const char*& pRecipientName, const char*& pRecipientEmail, const char*& pRecipientPhone, const char*& pRecipientAddress, const char*& pRecipientRelationWithInfoOwner, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::extractRecipientInfo");

	int retVal = 0;
	char log_msg[1024];

	recipientId = 0;
	json_t* pRecipientIdObject = json_object_get(pRecipientObject, IMA_JSON_KEY_ID);
	if(pRecipientIdObject != NULL) {
		if(json_is_integer(pRecipientIdObject) == true) {
			long temp = json_integer_value(pRecipientIdObject);
			if(temp > 0) {
				recipientId = temp;
			} else {
				sprintf(log_msg, "Not a valid recipient id for info id %lu", infoId);
				LOG(debug, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid recipient id for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}

	if((retVal == 0) && (recipientId > 0)) {
		LOG(debug, __FILE__, __LINE__, "legal recipient id found");
		return retVal;
	} else if(retVal < 0) {
		return retVal;
	}


	json_t* pRecipientNameObject = json_object_get(pRecipientObject, IMA_JSON_KEY_NAME);
	if(pRecipientNameObject != NULL) {
		if(json_is_string(pRecipientNameObject) == true) {
			pRecipientName = json_string_value(pRecipientNameObject);
		} else {
			sprintf(log_msg, "Not a valid recipient name for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}

	json_t* pRecipientEmailObject = json_object_get(pRecipientObject, IMA_JSON_KEY_EMAIL);
	if(pRecipientEmailObject != NULL) {
		if(json_is_string(pRecipientEmailObject) == true) {
			pRecipientEmail = json_string_value(pRecipientEmailObject);
			if(ImaUtils::isValidEmailAddress(pRecipientEmail) == false) {
				sprintf(log_msg, "Not a valid recipient email address(%s)", pRecipientEmail);
				LOG(debug, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid recipient recipient email id for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}

	json_t* pRecipientPhoneObject = json_object_get(pRecipientObject, IMA_JSON_KEY_PHONE);
	if(pRecipientPhoneObject != NULL) {
		if(json_is_string(pRecipientPhoneObject) == true) {
			pRecipientPhone = json_string_value(pRecipientPhoneObject);
			if(ImaUtils::isValidPhoneNum(pRecipientPhone) == false) {
				sprintf(log_msg, "Not a valid recipient phone num (%s)", pRecipientPhone);
				LOG(debug, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = -1;
			}
		} else {
			sprintf(log_msg, "Not a valid recipient phone num for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}
	
	json_t* pRecipientAddressObject = json_object_get(pRecipientObject, IMA_JSON_KEY_ADDRESS);
	if(pRecipientAddressObject != NULL) {
		if(json_is_string(pRecipientAddressObject) == true) {
			pRecipientAddress = json_string_value(pRecipientAddressObject);
		} else {
			sprintf(log_msg, "Not a valid recipient address for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}

	json_t* pRecipientRelationWithInfoOwnerObject = json_object_get(pRecipientObject, IMA_JSON_KEY_RELATION_WITH_OWNER);
	if(pRecipientRelationWithInfoOwnerObject!= NULL) {
		if(json_is_string(pRecipientRelationWithInfoOwnerObject) == true) {
			pRecipientRelationWithInfoOwner = json_string_value(pRecipientRelationWithInfoOwnerObject);
		} else {
			sprintf(log_msg, "Not a valid recipient relation with owner for info id %lu", infoId);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	if (((pRecipientName == NULL) || (strlen(pRecipientName)==0))
				&& ((pRecipientEmail == NULL) || (strlen(pRecipientEmail) == 0))
				&& ((pRecipientPhone == NULL) || (strlen(pRecipientPhone) == 0))
				&& ((pRecipientAddress == NULL) || (strlen(pRecipientAddress) == 0))
				&& ((pRecipientRelationWithInfoOwner == NULL) || (strlen(pRecipientRelationWithInfoOwner) == 0))) {
		sprintf(log_msg, "Atleast one of the id, name, email address or phone or address or relationship of the recipient with the info owner should be provided.");
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;	
}

int ImaRequestProcessor::extractRecipientInfo(json_t* pDetailsObject, const char*& pRecipientName, const char*& pRecipientEmailId, const char*& pRecipientPhone, const char*& pRecipientAddress, const char*& pRecipientRelationship, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::extractRecipientInfo called");

	int retVal = 0;
	
	/* Extract the name*/
	json_t* pNameObj = json_object_get(pDetailsObject, IMA_JSON_KEY_NAME);
	if(pNameObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Name field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pNameObj)) {
			pRecipientName = json_string_value(pNameObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid name"));
			retVal = -1;
		}
	}

	/* Extract the EmailID */
	json_t* pEmailObj = json_object_get(pDetailsObject, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Email field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pRecipientEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid Email ID"));
			retVal = -1;
		}
	}
	
	/* Extract the Phone number*/
	json_t* pPhoneObj = json_object_get(pDetailsObject, IMA_JSON_KEY_PHONE);
	if(pPhoneObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Phone field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pPhoneObj)) {
			pRecipientPhone = json_string_value(pPhoneObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid recipient Phone"));
			retVal = -1;
		}
	}

	/* Extract the Address*/
	json_t* pAddressObj = json_object_get(pDetailsObject, IMA_JSON_KEY_ADDRESS);
	if(pAddressObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pAddressObj)) {
			pRecipientAddress = json_string_value(pAddressObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid recipient Address"));
			retVal = -1;
		}
	}

	/* Extract the Relationship */
	json_t* pRelationObj = json_object_get(pDetailsObject, IMA_JSON_KEY_RELATION_WITH_OWNER);
	if(pRelationObj == NULL) {
		messagesVector.push_back(new std::string("Recipient relationship field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pRelationObj)) {
			pRecipientRelationship = json_string_value(pRelationObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid recipient relationship"));
			retVal = -1;
		}
	}

	return retVal;
}

int ImaRequestProcessor::processAddInfoRecipientsRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, std::string& strRecipientIds) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processAddInfoRecipientRequest called");

	int retVal = 0;

	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	//update the last access time first
	if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
		LOG(error, __FILE__, __LINE__, "Failed to update last access time");
	}

	/* Extract the info id object */
	unsigned long infoId = 0;
	json_t* pInfoIdObj = json_object_get(pRoot, IMA_JSON_KEY_INFO_ID);
	if(pInfoIdObj == NULL) {
		messagesVector.push_back(new std::string("Info Id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pInfoIdObj)) {
			infoId = json_integer_value(pInfoIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid info-id"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the info Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredInfo(infoId, pEmailId)) {
		sprintf(log_msg, "Info id %ld belonging to owner %s is registered", infoId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Info id %ld is not valid.", infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid info-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}
	
   json_t* pRecipientsObject = json_object_get(pRoot, IMA_JSON_KEY_RECIPIENTS);
   if(pRecipientsObject == NULL) {
      LOG(error, __FILE__, __LINE__, "Recipients object not present in JSON string");
		messagesVector.push_back(new std::string("Recipients object not present in JSON string"));
      retVal =  -1;
   } else {
      if(json_is_array(pRecipientsObject) == false) {
         LOG(debug, __FILE__, __LINE__, "Info recipients not in correct format");
			messagesVector.push_back(new std::string("Recipients object not in correct format"));
      	retVal =  -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	//loop into the recipient array and add all the recipient for the info to the DB
	size_t numRecipients = json_array_size(pRecipientsObject);
	if(numRecipients <=0) {
		LOG(debug, __FILE__, __LINE__, "Not a single recipient provided");
		messagesVector.push_back(new std::string("Not a single recipient provided for the info"));
		retVal = -1;	
	}

	if(retVal != 0) {
		return retVal;
	}

	unsigned int numOfRecipientsAdded = 0;
	for(unsigned int i = 0; i < numRecipients; i++) {
		unsigned long recipientId = 0;
		const char* pRecipientName = "";
		const char* pRecipientEmail = "";
		const char* pRecipientPhone = "";
		const char* pRecipientAddress = "";
		const char* pRecipientRelationWithInfoOwner = "";

		json_t* pRecipientObject = json_array_get(pRecipientsObject, i);
		if(!pRecipientObject) {
			sprintf(log_msg, "Not a proper recipient object at index %u", i);
			LOG(debug, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			continue;
		}

		if(extractRecipientInfo(infoId, pRecipientObject, recipientId, pRecipientName, pRecipientEmail, pRecipientPhone, pRecipientAddress, pRecipientRelationWithInfoOwner, messagesVector) == 0) {
			if(recipientId > 0) {
				if(1 != gpDbInterface->isRegisteredRecipient(recipientId, pEmailId)) {
					sprintf(log_msg, "The recipient id %lu is invalid", recipientId); 
					LOG(debug, __FILE__, __LINE__, log_msg);
					messagesVector.push_back(new std::string("Some of the recipient IDs are invalid"));
					break;
				} else {
					if(1 != gpDbInterface->addInfoRecipient(infoId, recipientId)) {
						sprintf(log_msg, "Failed to add recipient %lu for info %lu because of some DB error", recipientId, infoId);
						LOG(error, __FILE__, __LINE__, log_msg);
						messagesVector.push_back(new std::string(log_msg));
					} else {
						sprintf(log_msg, "Added recipient %lu for info %lu", infoId, recipientId);
						LOG(debug, __FILE__, __LINE__, log_msg);
						numOfRecipientsAdded++;
						//char strRecipientId[256];
						//sprintf(strRecipientId, "%lu", recipientId);
						//if(strRecipientIds.length() > 1) {
						//	strRecipientIds += ",";
						//}
						//strRecipientIds += strRecipientId;
					}
				}
			} else {
				if(1 == gpDbInterface->registerRecipient(pRecipientName, pRecipientEmail, pRecipientPhone, pRecipientAddress, pRecipientRelationWithInfoOwner, pEmailId)) {
					std::string strRecipientId;
					if(1 == gpDbInterface->fetchRecipientId(pRecipientName, pRecipientAddress, pRecipientEmail, pRecipientPhone, pRecipientRelationWithInfoOwner, pEmailId, strRecipientId)) {
						recipientId = atol(strRecipientId.c_str());
						if(1 != gpDbInterface->addInfoRecipient(infoId, recipientId)) {
							sprintf(log_msg, "Failed to add recipient %lu for info %lu because of some DB error", recipientId, infoId);
							LOG(error, __FILE__, __LINE__, log_msg);
							messagesVector.push_back(new std::string(log_msg));
						} else {
							sprintf(log_msg, "Added recipient %lu for info %lu", infoId, recipientId);
							LOG(debug, __FILE__, __LINE__, log_msg);
							numOfRecipientsAdded++;
							if(strRecipientIds.length() > 1) {
								strRecipientIds += ",";
							}
							strRecipientIds += strRecipientId;
						}
					} else {
						LOG(error, __FILE__, __LINE__, "Failed to fetch recipient id");
						messagesVector.push_back(new std::string("Failed to fetch recipient id"));
					}
				} else {
					sprintf(log_msg, "Failed to register recipient with email id: %s and phone %s for owner %s because of some DB error", pRecipientEmail, pRecipientPhone, pEmailId);
					messagesVector.push_back(new std::string(log_msg));
					LOG(error, __FILE__, __LINE__, log_msg);
				}
			}
		}
	}

	if(numOfRecipientsAdded <= 0) {
		sprintf(log_msg, "There was not a single recipient worth adding for the info %lu", infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = -1;
	} else {
		sprintf(log_msg, "No of recipients added for info id %lu is %u", infoId, numOfRecipientsAdded);
		LOG(debug, __FILE__, __LINE__, log_msg);
	}
	
	return retVal;
}

int ImaRequestProcessor::processAddRecipientRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, std::string& strRecipientId) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processAddRecipientRequest called");

	int retVal = 0;
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(debug, __FILE__, __LINE__, "Recipient details object not present in JSON string");
		messagesVector.push_back(new std::string("Recipient details object not present in JSON string"));
      retVal = -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(debug, __FILE__, __LINE__, "Recipient details info not in correct format");
			messagesVector.push_back(new std::string("Recipient details not in correct format"));
         retVal = -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	const char* pRecipientName = NULL;
	const char* pRecipientEmailId = NULL;
	const char* pRecipientPhone = NULL;
	const char* pRecipientAddress = NULL;
	const char* pRecipientRelationship = NULL;
	if(0 != extractRecipientInfo(pDetailsObject, pRecipientName, pRecipientEmailId, pRecipientPhone, pRecipientAddress, pRecipientRelationship, messagesVector)) {
		retVal = -1;
		LOG(debug, __FILE__, __LINE__, "Required Recipient info not present");
		messagesVector.push_back(new std::string("Required recipient info not present"));
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->registerRecipient(pRecipientName, pRecipientEmailId, pRecipientPhone, pRecipientAddress, pRecipientRelationship, pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The recipient %s added successfully for %s", pRecipientName, pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		if(1 == gpDbInterface->fetchRecipientId(pRecipientName, pRecipientAddress, pRecipientEmailId, pRecipientPhone, pRecipientRelationship, pEmailId, strRecipientId)) {
			char log_msg[1024];
			sprintf(log_msg, "The recipient id assigned for the recipient %s against user %s is %s", strRecipientId.c_str(), pRecipientName, pEmailId);
			LOG(debug, __FILE__, __LINE__, log_msg);
		} else {
			LOG(error, __FILE__, __LINE__, "Failed to fetch recipient id");
			messagesVector.push_back(new std::string("Failed to fetch recipient id"));
			retVal = -1;
		}

		//Also update the last access time of the user
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Failed to update last access time of the user");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The recipient %s failed to be added", pRecipientName);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processUpdateRecipientRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processUpdateRecipientRequest called");

	int retVal = 0;
	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	/* Extract the recipient id object */
	unsigned long recipientId = 0;
	json_t* pRecipientIdObj = json_object_get(pRoot, IMA_JSON_KEY_RECIPIENT_ID);
	if(pRecipientIdObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pRecipientIdObj)) {
			recipientId = json_integer_value(pRecipientIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid recipient-id"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the recipient Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredRecipient(recipientId, pEmailId)) {
		sprintf(log_msg, "Recipient id %lu belonging to owner %s is registered", recipientId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Recipient id %lu is not valid.", recipientId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid recipient-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(debug, __FILE__, __LINE__, "Recipient details object not present in JSON string");
		messagesVector.push_back(new std::string("Recipient details object not present in JSON string"));
      retVal = -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(debug, __FILE__, __LINE__, "Recipient details info not in correct format");
			messagesVector.push_back(new std::string("Recipient details not in correct format"));
         retVal = -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	const char* pRecipientName = NULL;
	const char* pRecipientEmailId = NULL;
	const char* pRecipientPhone = NULL;
	const char* pRecipientAddress = NULL;
	const char* pRecipientRelationship = NULL;
	if(0 != extractRecipientInfo(pDetailsObject, pRecipientName, pRecipientEmailId, pRecipientPhone, pRecipientAddress, pRecipientRelationship, messagesVector)) {
		retVal = -1;
		LOG(debug, __FILE__, __LINE__, "Required Recipient info not present");
		messagesVector.push_back(new std::string("Required recipient info not present"));
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->updateRecipient(recipientId, pRecipientName, pRecipientEmailId, pRecipientPhone, pRecipientAddress, pRecipientRelationship)) {
		char log_msg[512];
		sprintf(log_msg, "The recipient details of %lu successfully", recipientId);
		LOG(info, __FILE__, __LINE__, log_msg);

		//Also update the last access time of the user
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Failed to update last access time of the user");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The information of recipient %lu failed to be updated", recipientId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processDeleteRecipientRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processDeleteRecipientRequest called");

	int retVal = 0;
	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	/* Extract the recipient id object */
	unsigned long recipientId = 0;
	json_t* pRecipientIdObj = json_object_get(pRoot, IMA_JSON_KEY_RECIPIENT_ID);
	if(pRecipientIdObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pRecipientIdObj)) {
			recipientId = json_integer_value(pRecipientIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid recipient-id"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the recipient Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredRecipient(recipientId, pEmailId)) {
		sprintf(log_msg, "Recipient id %lu belonging to owner %s is registered", recipientId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Recipient id %lu is not valid.", recipientId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid recipient-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->deleteRecipient(recipientId, pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The recipient %lu deleted successfully", recipientId);
		LOG(info, __FILE__, __LINE__, log_msg);

		//Also update the last access time of the user
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(debug, __FILE__, __LINE__, "Failed to update last access time of the user");
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The information of recipient %lu failed to be deleted", recipientId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processDeleteInfoRecipientRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processDeleteInfoRecipientRequest called");

	int retVal = 0;

	char log_msg[1024];
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	//update the last access time first
	if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
		LOG(error, __FILE__, __LINE__, "Failed to update last access time because of some DB error");
	}
	
   json_t* pDetailsObject = json_object_get(pRoot, IMA_JSON_KEY_DETAILS);
   if(pDetailsObject == NULL) {
      LOG(debug, __FILE__, __LINE__, "Details object not present in JSON string");
		messagesVector.push_back(new std::string("Details object not present in JSON string"));
      retVal = -1;
   } else {
      if(json_is_object(pDetailsObject) == false) {
         LOG(debug, __FILE__, __LINE__, "Details not in correct format");
			messagesVector.push_back(new std::string("Details not in correct format"));
         retVal = -1;
      }
   }

	if(retVal != 0) {
		return retVal;
	}

	/* Extract the info id object */
	unsigned long infoId = 0;
	json_t* pInfoIdObj = json_object_get(pDetailsObject, IMA_JSON_KEY_INFO_ID);
	if(pInfoIdObj == NULL) {
		messagesVector.push_back(new std::string("Info Id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pInfoIdObj)) {
			infoId = json_integer_value(pInfoIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid info-id"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the info Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredInfo(infoId, pEmailId)) {
		sprintf(log_msg, "Info id %lu belonging to owner %s is registered", infoId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Info id %lu is not valid.", infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid info-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Extract the recipient id object */
	unsigned long recipientId = 0;
	json_t* pRecipientIdObj = json_object_get(pDetailsObject, IMA_JSON_KEY_RECIPIENT_ID);
	if(pRecipientIdObj == NULL) {
		messagesVector.push_back(new std::string("Recipient Id field not existing."));
		retVal = -1;
	} else {
		if(json_is_integer(pRecipientIdObj)) {
			recipientId = json_integer_value(pRecipientIdObj);
		} else {
			messagesVector.push_back(new std::string("Not a recipient-id"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	/* Now check if the recipient Id is registered in the Db or not */
	if(1 == gpDbInterface->isRegisteredRecipient(recipientId, pEmailId)) {
		sprintf(log_msg, "Recipient id %lu belonging to owner %s is registered", infoId, pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Recipient id %lu is not valid.", recipientId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string("Not a valid recipient-id"));
		retVal = -1;
	}

	if(retVal != 0) {
		return retVal;
	}

	if(gpDbInterface->deleteInfoRecipient(recipientId, infoId) == 1) {
		sprintf(log_msg, "Successfully deleted recipient %lu with info id %lu", recipientId, infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		retVal = 0;
	} else {
		sprintf(log_msg, "Failed to delete recipient %lu with info id %lu because of some DB error", recipientId, infoId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processUpdateLastAccessTimeRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processUpdateLastAccessTimeRequest called");

	int retVal = 0;
	
	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
			if(!ImaUtils::isValidEmailAddress(pEmailId)) {
				messagesVector.push_back(new std::string("Not a valid User Email Address"));
				retVal = -1;
			}
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	if(1 == gpDbInterface->updateLastAccessTime(pEmailId)) {
		char log_msg[512];
		sprintf(log_msg, "The last access time of %s successfully updated", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = 0;
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to update the last access time of user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processChangePasswordRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processChangePasswordRequest called");	

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	/* Extract and validate the user password */
	const char* pPassword= NULL;
	json_t* pPasswordObj = json_object_get(pRoot, IMA_JSON_KEY_PASSWORD);
	if(pPasswordObj == NULL) {
		messagesVector.push_back(new std::string("Password field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pPasswordObj)) {
			pPassword = json_string_value(pPasswordObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid password"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	if(1 == gpDbInterface->areUserCredentialsCorrect(pEmailId, pPassword)) {
		char log_msg[512];
		sprintf(log_msg, "The credentials of user id %s are correct", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		
		//Also update the last access time
		if(0 > gpDbInterface->updateLastAccessTime(pEmailId)) {
			LOG(error, __FILE__, __LINE__, "Failed to update last access time");
		}

	} else {
		char log_msg[1024];
		sprintf(log_msg, "The credentials of user %s are incorrect", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	/* Extract and validate the new password */
	const char* pNewPassword= NULL;
	json_t* pNewPasswordObj = json_object_get(pRoot, IMA_JSON_KEY_NEW_PASSWORD);
	if(pNewPasswordObj == NULL) {
		messagesVector.push_back(new std::string("New Password field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pNewPasswordObj)) {
			pNewPassword = json_string_value(pNewPasswordObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid new password"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	if(1 == gpDbInterface->changePassword(pEmailId, pPassword, pNewPassword)) {
		char log_msg[512];
		sprintf(log_msg, "Password of user %s updated", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to update password of user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processForgotPasswordRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processForgotPasswordRequest called");	

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	if(1 == gpDbInterface->isRegisteredUser(pEmailId)) {
		char log_msg[1024];
		sprintf(log_msg, "The user %s is registered", pEmailId);
		LOG(debug, __FILE__, __LINE__, log_msg);
		/* Generate new password and set the same into the DB */
		char newPassword[128];
		ImaUtils::genRandomKey(newPassword, 6);
		if(gpDbInterface->resetPassword(pEmailId, newPassword) == 1) {
			LOG(debug, __FILE__, __LINE__, "The new password has been reset into the DB")
			/* Also Enqueue the reset password into the message queue so that the same can be delivered 
			to the user via email */
			if(gpMqInterface->enqueueNewPassword(pEmailId, newPassword)) {

				sprintf(log_msg, "Your password has been sent to your email ID %s", pEmailId);
				LOG(info, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
			} else {
				sprintf(log_msg, "Failed to send new password of email ID %s into the message queue", pEmailId);
				LOG(info, __FILE__, __LINE__, log_msg);
				messagesVector.push_back(new std::string(log_msg));
				retVal = -1;
			}
		} else {
			char log_msg[1024];
			sprintf(log_msg, "Failed to set the new password of the user %s into the DB", pEmailId);
			LOG(error, __FILE__, __LINE__, log_msg);
			messagesVector.push_back(new std::string(log_msg));
			retVal = -1;
		}
	} else {
		char log_msg[1024];
		sprintf(log_msg, "The user email id %s is not registered", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

int ImaRequestProcessor::processGetUserDetailsRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, char*& pResponse) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processGetUserDetailsRequest");

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	std::string strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService;
	if(gpDbInterface->fetchUserDetails(pEmailId, strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService) != 1) {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch user details for the user id because of some DB issue: %s", pEmailId);
		messagesVector.push_back(new std::string(log_msg));
		LOG(error, __FILE__, __LINE__, log_msg);
		retVal = -1;
	} else {
		LOG(debug, __FILE__, __LINE__, "Fetched user details");
	}
	
	pResponse = assembleUserDetailsResponse(IMA_ACTION_GET_USER_DETAILS, retVal, (retVal == 0) ? IMA_JSON_VALUE_SUCCESS : IMA_JSON_VALUE_FAILURE, messagesVector, pEmailId, strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService);

	return retVal;
}

int ImaRequestProcessor::processFetchUserDataRequest(json_t* pRoot, std::vector<std::string*>& messagesVector, char*& pResponse) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processFetchUserDataRequest");

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}

	std::string strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService;
	if(gpDbInterface->fetchUserDetails(pEmailId, strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService) != 1) {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch user details for the user id: %s. Please try again", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	} else {
		LOG(debug, __FILE__, __LINE__, "Fetched user details");
	}

	std::vector<RECIPIENT_DETAILS*> recipientList;
	if(1 == gpDbInterface->fetchRecipientList(pEmailId, recipientList)) {
		char log_msg[512];
		sprintf(log_msg, "Fetched list of recipients for the user %s", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch list of recipients for the user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	std::vector<INFO_DETAILS*> infoList;
	if(1 == gpDbInterface->fetchInfoList(pEmailId, infoList)) {
		char log_msg[512];
		sprintf(log_msg, "Fetched list of info for the user %s", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch list of info for the user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	pResponse = assembleUserDataResponse(IMA_ACTION_FETCH_USER_DATA, retVal, (retVal == 0) ? IMA_JSON_VALUE_SUCCESS : IMA_JSON_VALUE_FAILURE, messagesVector, pEmailId, strName, strAddress, strCity, strState, strCountry, strZip, strPhone, strDob, strGender, strImId, strImService, recipientList, infoList);

	return retVal;
}


int ImaRequestProcessor::processGetRecipientsListRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processGetRecipientsListRequest");

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	std::vector<RECIPIENT_DETAILS*> recipientList;
	if(1 == gpDbInterface->fetchRecipientList(pEmailId, recipientList)) {
		char log_msg[512];
		sprintf(log_msg, "Fetched list of recipients for the user %s", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch list of recipients for the user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}
	return retVal;
}

int ImaRequestProcessor::processGetInfoListRequest(json_t* pRoot, std::vector<std::string*>& messagesVector) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::processGetInfoListRequest");

	int retVal = 0;

	/* Extract and validate the user email address */
	const char* pEmailId = NULL;
	json_t* pEmailObj = json_object_get(pRoot, IMA_JSON_KEY_EMAIL);
	if(pEmailObj == NULL) {
		messagesVector.push_back(new std::string("User email address field not existing."));
		retVal = -1;
	} else {
		if(json_is_string(pEmailObj)) {
			pEmailId = json_string_value(pEmailObj);
		} else {
			messagesVector.push_back(new std::string("Not a valid User Email Address"));
			retVal = -1;
		}
	}

	if(retVal != 0) {
		return retVal;
	}
	
	std::vector<INFO_DETAILS*> infoList;
	if(1 == gpDbInterface->fetchInfoList(pEmailId, infoList)) {
		char log_msg[512];
		sprintf(log_msg, "Fetched list of info for the user %s", pEmailId);
		LOG(info, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
	} else {
		char log_msg[1024];
		sprintf(log_msg, "Failed to fetch list of info for the user %s", pEmailId);
		LOG(error, __FILE__, __LINE__, log_msg);
		messagesVector.push_back(new std::string(log_msg));
		retVal = -1;
	}

	return retVal;
}

char* ImaRequestProcessor::processRequest(const char* pJsonString) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::ProcessRequest called");

	if(pJsonString == NULL) {
		LOG(debug, __FILE__, __LINE__, "pJsonString is NULL");
		return NULL;
	}
	int retVal = -1;
	json_t* pRoot = NULL;
	json_error_t error;
	std::vector<std::string*> messageVector;
	string strId;

	pRoot = json_loads(pJsonString, 0, &error);
	if(!pRoot) {
		char log_msg[4096];
		snprintf(log_msg, 4096, "Failed to parse json string %s\n; error: %s\n", pJsonString, error.text);
		LOG(debug, __FILE__, __LINE__, log_msg);
		messageVector.push_back(new std::string(log_msg));
		char* pResp = assembleResponsePkt(IMA_JSON_VALUE_UNKNOWN, IMA_JSON_VALUE_FAILURE, retVal, messageVector, strId);
		return pResp;	
	}

	int nAction = IMA_JSON_VALUE_UNKNOWN;
	json_t* pActionObj = json_object_get(pRoot, IMA_JSON_KEY_ACTION);
	if(json_is_integer(pActionObj)) {
		char* pUserDetailsResponse = NULL;
		char* pRecipientListResponse = NULL;
		char* pInfoListResponse = NULL;
		char* pUserDataResponse = NULL;
		nAction = json_integer_value(pActionObj);
		switch(nAction) {
			case IMA_ACTION_REGISTER:
				retVal = processRegistrationRequest(pRoot, messageVector, strId);
				break;
			case IMA_ACTION_CONFIRM_REGISTRATION:
				retVal = processConfirmRegistrationRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_UPDATE_USER_DETAILS:
				retVal = processUpdateUserDetailsRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_LOGIN:
				retVal = processLoginRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_ADD_INFO:
				retVal = processAddInfoRequest(pRoot, messageVector, strId);
				break;
			case IMA_ACTION_UPDATE_INFO:
				retVal = processUpdateInfoRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_DELETE_INFO:
				retVal = processDeleteInfoRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_ADD_INFO_RECIPIENTS:
				retVal = processAddInfoRecipientsRequest(pRoot, messageVector, strId);
				break;
			case IMA_ACTION_DELETE_INFO_RECIPIENT:
				retVal = processDeleteInfoRecipientRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_UPDATE_RECIPIENT:
				retVal = processUpdateRecipientRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_ADD_RECIPIENT:
				retVal = processAddRecipientRequest(pRoot, messageVector, strId);
				break;
			case IMA_ACTION_DELETE_RECIPIENT:
				retVal = processDeleteRecipientRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_UPDATE_LAST_ACCESS_TIME:
				retVal = processUpdateLastAccessTimeRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_CHANGE_PASSWORD:
				retVal = processChangePasswordRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_FORGOT_PASSWORD:
				retVal = processForgotPasswordRequest(pRoot, messageVector);
				break;
			case IMA_ACTION_GET_USER_DETAILS:
			//	char* pUserDetailsResponse = NULL;
				retVal = processGetUserDetailsRequest(pRoot, messageVector, pUserDetailsResponse);
				json_decref(pRoot);
				return pUserDetailsResponse;
			case IMA_ACTION_FETCH_USER_DATA:
				retVal = processFetchUserDataRequest(pRoot, messageVector, pUserDataResponse);
				json_decref(pRoot);
				return pUserDataResponse;
			case IMA_ACTION_USER_FEEDBACK:
				retVal = processFeedbackRequest(pRoot, messageVector);
				break;
			//case IMA_ACTION_GET_RECIPIENTS_LIST:
			//	retVal = processGetRecipientsListRequest(pRoot, messageVector, pRecipientListResponse);
			//	json_decref(pRoot);
			//	return pRecipientListResponse;
			//case IMA_ACTION_GET_INFO_LIST:
			//	retVal = processGetInfoListRequest(pRoot, messageVector, pInfoListResponse);
			//	json_decref(pRoot);
			//	return pInfoListResponse;
			default:
				//TODO: Unknown request
				char log_msg[256];
				sprintf(log_msg, "Unknown request %d", nAction);
				LOG(debug, __FILE__, __LINE__, log_msg);
				messageVector.push_back(new std::string(log_msg));
				break;
		}
	}
	
	json_decref(pRoot);

	return assembleResponsePkt(nAction, retVal, (retVal == 0) ? IMA_JSON_VALUE_SUCCESS : IMA_JSON_VALUE_FAILURE, messageVector, strId);
}

char* ImaRequestProcessor::assembleUserDetailsResponse(int nAction, int nResult, int nStatusCode, std::vector<std::string*>& msgVector, const char* pEmailId, std::string& strName, std::string& strAddress, std::string& strCity, std::string& strState, std::string& strCountry, std::string& strZip, std::string& strPhone, std::string& strDob, std::string& strGender, std::string& strImId, std::string& strImService) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::assembleUserDetailsResponse called");

	json_t* pRootObj = json_object();
	json_object_set_new(pRootObj, IMA_JSON_KEY_TYPE, json_string(IMA_JSON_VALUE_RESPONSE));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(IMA_JSON_VALUE_APPSERVER)); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION, json_integer(nAction));	
	json_object_set_new(pRootObj, IMA_JSON_KEY_RESULT, json_integer(nResult));
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_CODE, json_integer(nStatusCode));
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL, json_string(pEmailId)); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_NAME, json_string(strName.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ADDRESS, json_string(strAddress.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_CITY, json_string(strCity.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATE, json_string(strState.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_COUNTRY, json_string(strCountry.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ZIP, json_string(strZip.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_PHONE, json_string(strPhone.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_DOB, json_string(strDob.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_GENDER, json_string(strGender.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_IM_ID, json_string(strImId.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_IM_SERVICE, json_string(strImService.c_str())); 

	/* Iterate through the messages vector and add them into the messages 
		array in the json response packet */

	//TODO: check the size of the vector first
	json_t* pJsonStrArray = json_array();
	std::vector<std::string*>::iterator itr;
	for(itr = msgVector.begin(); itr != msgVector.end(); itr++) {
		json_array_append_new(pJsonStrArray, json_string((*itr)->c_str()));
		delete(*itr);
	}
	msgVector.clear();
	
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_MESSAGES, pJsonStrArray);
	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));

	json_decref(pJsonStrArray);
	json_decref(pRootObj);

	char log_msg[4096];	
	snprintf(log_msg, 4096, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaRequestProcessor::assembleUserDataResponse(int nAction, int nResult, int nStatusCode, std::vector<std::string*>& msgVector, const char* pEmailId, std::string& strName, std::string& strAddress, std::string& strCity, std::string& strState, std::string& strCountry, std::string& strZip, std::string& strPhone, std::string& strDob, std::string& strGender, std::string& strImId, std::string& strImService, std::vector<RECIPIENT_DETAILS*>& recipientList, std::vector<INFO_DETAILS*>& infoList) {
	LOG(debug, __FILE__, __LINE__, "ImaRequestProcessor::assembleUserDataResponse called");

	json_t* pRootObj = json_object();
	json_object_set_new(pRootObj, IMA_JSON_KEY_TYPE, json_string(IMA_JSON_VALUE_RESPONSE));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(IMA_JSON_VALUE_APPSERVER)); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION, json_integer(nAction));	
	json_object_set_new(pRootObj, IMA_JSON_KEY_RESULT, json_integer(nResult));
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_CODE, json_integer(nStatusCode));

	//User's Details
	json_t* pUserDetailsObject = json_object();
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_EMAIL, json_string(pEmailId)); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_NAME, json_string(strName.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_ADDRESS, json_string(strAddress.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_CITY, json_string(strCity.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_STATE, json_string(strState.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_COUNTRY, json_string(strCountry.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_ZIP, json_string(strZip.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_PHONE, json_string(strPhone.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_DOB, json_string(strDob.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_GENDER, json_string(strGender.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_IM_ID, json_string(strImId.c_str())); 
	json_object_set_new(pUserDetailsObject, IMA_JSON_KEY_IM_SERVICE, json_string(strImService.c_str()));

	json_object_set_new(pRootObj, IMA_JSON_KEY_USER_DETAILS, pUserDetailsObject);


	//List of recipients corresponding to the user
	json_t* pJsonRecipientArray = json_array();
	std::vector<RECIPIENT_DETAILS*>::iterator recipientIter;
	for(recipientIter = recipientList.begin(); recipientIter != recipientList.end(); recipientIter++) {
		RECIPIENT_DETAILS* pRecDetails = *(recipientIter);

		json_t* pJsonRecipientObject = json_object();
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_ID, json_integer(pRecDetails->id));
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_NAME, json_string(pRecDetails->name));
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_EMAIL, json_string(pRecDetails->emailId));
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_PHONE, json_string(pRecDetails->phone));
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_ADDRESS, json_string(pRecDetails->address));
		json_object_set_new(pJsonRecipientObject, IMA_JSON_KEY_RELATION_WITH_OWNER, json_string(pRecDetails->relationship));

		json_array_append_new(pJsonRecipientArray, pJsonRecipientObject);

		delete pRecDetails;
	}
	recipientList.clear();

	json_object_set_new(pRootObj, IMA_JSON_KEY_RECIPIENT_LIST, pJsonRecipientArray);

	//List of the info corresponding to the user
	json_t* pJsonInfoArray = json_array();
	std::vector<INFO_DETAILS*>::iterator infoIter;
	for(infoIter = infoList.begin(); infoIter != infoList.end(); infoIter++) {
		INFO_DETAILS* pInfoDetails = *(infoIter);

		json_t* pJsonInfoObject = json_object();
		json_object_set_new(pJsonInfoObject, IMA_JSON_KEY_ID, json_integer(pInfoDetails->id));
		json_object_set_new(pJsonInfoObject, IMA_JSON_KEY_INFO, json_string(pInfoDetails->info));
		json_object_set_new(pJsonInfoObject, IMA_JSON_KEY_MEDIA, json_integer(pInfoDetails->media));
		json_object_set_new(pJsonInfoObject, IMA_JSON_KEY_TTL, json_integer(pInfoDetails->ttl));

		//Add list of recipients for this INFO
		json_t* pJsonRecipientsIdsArray = json_array();
		std::vector<unsigned long>::iterator recipientIdIter;
		for(recipientIdIter = pInfoDetails->recipients.begin(); recipientIdIter != pInfoDetails->recipients.end(); recipientIdIter++) {
			unsigned long recipientId = *recipientIdIter;
			json_array_append_new(pJsonRecipientsIdsArray, json_integer(recipientId));
		}
		pInfoDetails->recipients.clear();

		json_object_set_new(pJsonInfoObject, IMA_JSON_KEY_RECIPIENTS, pJsonRecipientsIdsArray);

		json_array_append_new(pJsonInfoArray, pJsonInfoObject);

		delete pInfoDetails;
	}
	infoList.clear();
	json_object_set_new(pRootObj, IMA_JSON_KEY_INFO_LIST, pJsonInfoArray);

	/* Iterate through the messages vector and add them into the messages 
		array in the json response packet */
	//TODO: check the size of the vector first
	json_t* pJsonStrArray = json_array();
	std::vector<std::string*>::iterator itr;
	for(itr = msgVector.begin(); itr != msgVector.end(); itr++) {
		json_array_append_new(pJsonStrArray, json_string((*itr)->c_str()));
		delete(*itr);
	}
	msgVector.clear();

	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_MESSAGES, pJsonStrArray);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));

	json_decref(pJsonStrArray);
	json_decref(pRootObj);

	char log_msg[4096];	
	snprintf(log_msg, 4096, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaRequestProcessor::assembleResponsePkt(
			int nAction,
			int nResult,
			int nStatusCode,
			std::vector<std::string*>& msgVector,
			string& id) {

	char log_msg[4096];
	sprintf(log_msg, "ImaRequestProcessor::assembleResponsePkt called with action = %d, nResult = %d, nStatusCode = %d, id = %s", nAction, nResult, nStatusCode, id.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
	json_object_set_new(pRootObj, IMA_JSON_KEY_TYPE, json_string(IMA_JSON_VALUE_RESPONSE));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(IMA_JSON_VALUE_APPSERVER)); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION, json_integer(nAction));	
	json_object_set_new(pRootObj, IMA_JSON_KEY_RESULT, json_integer(nResult));
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_CODE, json_integer(nStatusCode));

	/* Iterate through the messages vector and add them into the messages 
		array in the json response packet */

	//TODO: check the size of the vector first
	json_t* pJsonStrArray = json_array();
	std::vector<std::string*>::iterator itr;
	for(itr = msgVector.begin(); itr != msgVector.end(); itr++) {
		json_array_append_new(pJsonStrArray, json_string((*itr)->c_str()));
		delete(*itr);
	}
	msgVector.clear();
	
	json_object_set_new(pRootObj, IMA_JSON_KEY_STATUS_MESSAGES, pJsonStrArray);
	json_object_set_new(pRootObj, IMA_JSON_KEY_ID, json_string(id.c_str()));
	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pJsonStrArray);
	json_decref(pRootObj);
	
	snprintf(log_msg, 4096, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}
